<template>
  <div class="chat-container">
    <!-- 左侧访客列表 -->
    <a-layout-sider width="200" style="background: #fff">
      <a-list :data-source="visitors">
        <template #renderItem="{ item }">
          <a-list-item>
            <a-avatar :src="item.avatar" />
            {{ item.name }}
            <a-badge v-if="item.unread > 0" :count="item.unread" />
          </a-list-item>
        </template>
      </a-list>
    </a-layout-sider>

    <!-- 右侧聊天窗口 -->
    <a-layout-content style="padding: 24px; background: #fff; min-height: 280px;">
      <div class="chat-header">
        <span>在线咨询</span>
        <a-button type="text" @click="toggleMinimize">
          <template #icon>
            <Icon v-if="!isMinimized" icon="line-md:window-minimize" />
            <Icon v-else icon="line-md:window-maximize" />
          </template>
        </a-button>
        <a-button type="text" @click="closeChat">
          <template #icon>
            <Icon icon="mdi:close" />
          </template>
        </a-button>
      </div>

      <!-- 聊天记录 -->
      <div class="chat-messages">
        <div v-for="(message, index) in messages" :key="index" :class="['message', message.sender === 'visitor' ? 'visitor' : 'staff']">
          <a-avatar :src="message.avatar" />
          <div class="message-content">
            <p>{{ message.text }}</p>
            <small>{{ message.timestamp }}</small>
          </div>
        </div>
      </div>

      <!-- 输入框和发送按钮 -->
      <div class="chat-input">
        <a-input v-model="newMessage" placeholder="输入消息..." @pressEnter="sendMessage" />
        <a-button type="primary" @click="sendMessage">发送</a-button>
      </div>
    </a-layout-content>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import { Icon } from '@arco-design/web-vue';

const visitors = [
  { name: '访客', avatar: 'https://placehold.co/50x50', unread: 2 },
  { name: '访客', avatar: 'https://placehold.co/50x50', unread: 3 },
  { name: '访客', avatar: 'https://placehold.co/50x50', unread: 0 },
  { name: '访客', avatar: 'https://placehold.co/50x50', unread: 0 }
];

const isMinimized = ref(false);
const newMessage = ref('');
const messages = ref([
  {
    sender: 'visitor',
    text: '在吗？',
    timestamp: '2020-12-1 23:59:0',
    avatar: 'https://placehold.co/50x50'
  },
  {
    sender: 'staff',
    text: '在的',
    timestamp: '2020-12-1 23:59:9',
    avatar: 'https://placehold.co/50x50'
  },
  {
    sender: 'visitor',
    text: '在哪儿呢？',
    timestamp: '2020-12-1 0:0:22',
    avatar: 'https://placehold.co/50x50'
  }
]);

const toggleMinimize = () => {
  isMinimized.value = !isMinimized.value;
};

const closeChat = () => {
  // 关闭聊天逻辑
};

const sendMessage = () => {
  if (newMessage.value.trim() !== '') {
    messages.value.push({
      sender: 'visitor',
      text: newMessage.value,
      timestamp: new Date().toLocaleString(),
      avatar: 'https://placehold.co/50x50'
    });
    newMessage.value = '';
  }
};
</script>

<style scoped>
.chat-container {
  display: flex;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px;
  background-color: #40a9ff;
  color: white;
}

.chat-messages {
  height: 300px;
  overflow-y: auto;
  padding: 10px;
}

.message {
  display: flex;
  margin-bottom: 10px;
}

.message.visitor .message-content {
  background-color: #00bfa5;
  color: white;
}

.message.staff .message-content {
  background-color: #64b5f6;
  color: white;
  margin-left: auto;
}

.message-content {
  padding: 10px;
  border-radius: 5px;
  max-width: 70%;
}

.chat-input {
  display: flex;
  padding: 10px;
}

.chat-input .arco-input {
  flex-grow: 1;
  margin-right: 10px;
}
</style>